package com.wondersgroup.fjzlpt.console.service.impl.role;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.console.constant.RoleScope;
import com.wondersgroup.fjzlpt.console.dao.inter.menu.MenuDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleMenuActionDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleMenuDAO;
import com.wondersgroup.fjzlpt.console.service.inter.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
@Service
@Transactional 
public class RoleServiceImpl implements RoleService{
	
	@Autowired
    private RoleDAO roleDao;
	@Autowired
	private RoleMenuDAO roleMenuDao;
	@Autowired
	private RoleMenuActionDAO roleMenuActionDAO;
	@Autowired
	private MenuDAO menuDao;
	
	/**
	 * 查询初始化数据
	 * @return
	 */
	public JSONObject queryInitData(Map map)
	{
		JSONObject json = new JSONObject();
		JSONArray menuList = this.menuDao.queryMenuAndActions();
		json.put("menuList", menuList);
		json.put("roleScope", RoleScope.getAll());
		String roleId = (String)map.get("ROLEID");
		if(!StringUtils.isEmpty(roleId))
		{
			JSONArray roleMenu = this.roleMenuDao.queryMenusByRoleId(roleId);
			json.put("roleMenu", roleMenu);
			JSONArray roleMenuAction = this.roleMenuActionDAO.queryByRoleId(roleId);
			json.put("roleMenuAction", roleMenuAction);
		}
		return json;
	}
	
	@Override
	public JSONObject save(Map<String,String> map) {
		ResultObject ro = new ResultObject();
		String roleId = map.get("ROLEID");
		if(StringUtils.isEmpty(roleId))
		{
			insert(map, ro);
		}
		else
		{
			update(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	private void insert(Map map, ResultObject ro)
	{
		try
		{
			String roleId = UUID.randomUUID().toString().toUpperCase();
			map.put("ROLEID", roleId);
			this.roleDao.insert(map);
			String menuIds = (String)map.remove("menuIds");
			String menuNavigates = (String)map.remove("menuNavigates");
			/*插入角色菜单*/
			insertBathRoleMenu(menuIds, roleId,menuNavigates);
			String menuActions = (String)map.remove("menuActions");
			/*插入角色菜单功能*/
			insertBatchRoleMenuAction(roleId, menuActions);
		}
		catch(Exception e)
		{
			ro.changeFaultState("新增角色失败");
			throw new RuntimeException("新增角色失败", e);
		}
	}
	
	public void update(Map map, ResultObject ro) {
		try
		{
			this.roleDao.updateForPrimaryKey(map);
			String roleId = (String)map.get("ROLEID");
			this.roleMenuDao.deleteByRoleId(roleId);
			String menuIds = (String)map.remove("menuIds");
			String menuNavigates = (String)map.remove("menuNavigates");
			insertBathRoleMenu(menuIds, roleId,menuNavigates);
			this.roleMenuActionDAO.deleteByRoleId(roleId);
			String menuActions = (String)map.remove("menuActions");
			/*插入角色菜单功能*/
			insertBatchRoleMenuAction(roleId, menuActions);
		}
		catch(Exception e)
		{
			ro.changeFaultState("修改角色失败");
			throw new RuntimeException("修改角色失败", e);
		}
	}

	/**
	 * 插入角色菜单
	 * @param menuIds
	 * @param roleId
	 */
	private void insertBathRoleMenu(String menuIds, String roleId,String navigates)
	{
		List<Map> list = new ArrayList<Map>();
		String[] menuIdAry = menuIds.split(",");
		String[] navigateAry = navigates.split(",");
		for(int i = 0; i < menuIdAry.length ; i ++ )
		{
			Map roleMenuMap = new HashMap<>();
			roleMenuMap.put("roleId", roleId);
			roleMenuMap.put("menuId", menuIdAry[i]);
			roleMenuMap.put("NAVIGATE", navigateAry[i]);
			list.add(roleMenuMap);
		}
		this.roleMenuDao.insertBatch(list);
	}

	/**
	 * 插入角色菜单功能
	 * @param roleId
	 * @param menuActions
	 */
	private void insertBatchRoleMenuAction(String roleId, String menuActions)
	{
		JSONArray ary = JSON.parseArray(menuActions);
		this.roleMenuActionDAO.inserBatch(roleId, ary);
	}

	@Override
	public JSONObject queryPage(Map<String, String> map) {
		return roleDao.queryPage(map);
	}
	
	/**
	 * 角色删除
	 * 1、删除角色表
	 * 2、删除角色菜单表
	 * 3、删除角色人员表
	 */
	@Override
	public JSONObject delete(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		try{
			roleDao.deleteForPrimaryKey(map);
			String roleId = map.get("ROLEID");
			this.roleMenuDao.deleteByRoleId(roleId);
			this.roleMenuActionDAO.deleteByRoleId(roleId);
		}catch(Exception e){
			ro.changeFaultState("删除角色失败");
			throw new RuntimeException("删除角色失败", e);
		}
		return (JSONObject)JSON.toJSON(ro);
	}

}
